package com.eliza.lifecycle.activity

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ProcessLifecycleOwner
import com.eliza.lifecycle.R
import com.eliza.lifecycle.observer.NewDefaultLcObserver
import com.eliza.lifecycle.service.MyService

/*

* https://developer.android.google.cn/guide/components/bound-services?hl=zh-cn
* */
class ServiceActivity : AppCompatActivity() {
    private lateinit var mService: MyService
    private var mBound: Boolean = false

    /** Defines callbacks for service binding, passed to bindService()  */
    private val connection = object : ServiceConnection {

        override fun onServiceConnected(className: ComponentName, service: IBinder) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            val binder = service as MyService.LocalBinder
            mService = binder.getService()
            mBound = true
        }

        override fun onServiceDisconnected(arg0: ComponentName) {
            mBound = false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ProcessLifecycleOwner.get().lifecycle.addObserver(
            NewDefaultLcObserver()
        )
        setContentView(R.layout.activity_service)

        /** Called when a button is clicked (the button in the layout file attaches to
         * this method with the android:onClick attribute)  */

        findViewById<Button>(R.id.service_status).setOnClickListener {
            if (mBound) {
                // Call a method from the LocalService.
                // However, if this call were something that might hang, then this request should
                // occur in a separate thread to avoid slowing down the activity performance.
                val num: Int = mService.randomNumber
                Toast.makeText(this, "number: $num", Toast.LENGTH_SHORT).show()
            }
        }
        findViewById<Button>(R.id.service_lock).setOnClickListener {
            // Bind to LocalService
            Intent(this, MyService::class.java).also { intent ->
                bindService(intent, connection, Context.BIND_AUTO_CREATE)
            }
        }

        findViewById<Button>(R.id.service_unlock).setOnClickListener {
            // Bind to LocalService
            unbindService(connection)
            mBound = false
        }


    }

    override fun onStart() {
        super.onStart()
        // Bind to LocalService
        Intent(this, MyService::class.java).also { intent ->
            bindService(intent, connection, Context.BIND_AUTO_CREATE)
        }
    }

    override fun onStop() {
        super.onStop()
        unbindService(connection)
        mBound = false
    }


}